/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.tan.handler;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.room.analyse.Card;
import extension.room.tan.TanExtension;
import extension.room.tan.TanGame;
import extension.room.tan.TanPlayer;
import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public class FinishAttackHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        TanExtension ext = (TanExtension) getParentExtension();
        TanGame game = (TanGame) ext.getGame();
        TanPlayer p = game.getPlayerFight();
        if (p.getUser().getName().equals(user.getName())) {
            if (game.getFightState() == TanGame.ATTACK) {
                p.setAttackState(TanPlayer.SKIP);
                Debug.d(p.getName() + " stop attack");
            } else {
                for (Integer cardAdd : game.getListCardOnTable()) {
                    p.addCard(cardAdd);
                }
                p.updateNumberCardInHand();
                Debug.d(p.getName() + " en fold card");
                logShowSet(p.getName(), game.getListCardOnTable());
                ext.getResponse().sendEnFoldCard(game.getListCardOnTable(), p.getSeatPos(), ext.getParentRoom().getUserList());
                p.setAttackState(TanPlayer.DEFENSE_LOST);
                game.findNextAttack();
            }
            game.nextTurn();
        }
    }

    public void logShowSet(String name, List<Integer> listPhom) {
        if (listPhom != null && !listPhom.isEmpty()) {
            StringBuilder builder = new StringBuilder();
            for (Integer card : listPhom) {
                builder.append(Card.getName(card));
                builder.append(",");
            }
            Debug.d(name + "-card:" + builder.toString());
        }
    }
}
